<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="品牌名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="success" icon="el-icon-search" @click="func_handleAdd" plain>添加</el-button>
            </div>
            <el-table :data="boardTableData" border class="table" header-cell-class-name="table-header">
                <el-table-column prop="id" label="ID" width="55" align="center" v-if="false" />
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="boardName" label="品牌名称"></el-table-column>
                <el-table-column label="品牌LOGO(查看大图)" align="center">
                    <template #default="scope">
                        <el-image class="table-td-thumb" :src="scope.row.boardLogo" :preview-src-list="[scope.row.boardLogo]">
                        </el-image>
                    </template>
                </el-table-column>
				<el-table-column prop="boardEnname" label="品牌英字"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                            @click="func_handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="func_handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editDialogVisible" width="55%" :close-on-click-modal="false">
            <VBoard v-bind:selectValue="vBoardData" @borderdata="func_loadvBoardData" />
        </el-dialog>
    </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { findBoardInfoListPage, deleteBoardInfoById, } from "../../../api/hairmanager/board";
import VBoard from "./components/boardForm.vue";
export default {
    name: "borderTable",
    components: {VBoard, },
    setup() {
        const router = useRouter();
		const route = useRoute();
        const optColumnWidth = 150;
        const query = reactive({
            keyword: "",
            pageNum: 1,
            pageSize: 10,
        });
        const boardTableData = ref([]);
        const dataTotal = ref(0);
        const vBoardData = reactive({
            load: false,
            id: 0,
        });
        // 表格编辑时弹窗和保存
        const editDialogVisible = ref(false);
        let idx = -1;
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findBoardInfoListPage(query).then((res) => {
                boardTableData.value = res.data.list;
                dataTotal.value = res.dataTotal;
            });
        };
        func_getData();
        /**
         * @name 主表格查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const func_handlePageChange = (val) => {
            query.pageNum = val;
            func_getData();
        };
        /**
         * @name 删除操作
         */
        const func_handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
            type: "warning",
            }).then(() => {
                func_del(row['id']);
            }).catch(() => {});
        };
        /**
         * @name 添加数据
         */
        const func_handleAdd = () => {
            vBoardData['id'] = 0;
            vBoardData['load'] = false;
            editDialogVisible.value = true;
        };
        /**
         * @name 行编辑操作
         */
        const handleEdit = (index, row) => {
            idx = index;
            vBoardData['id'] = row['id'];
            vBoardData['load'] = true;
            editDialogVisible.value = true;
        };
        const func_loadvBoardData = (v) => {
            if(v['state'] === 1){
                editDialogVisible.value = false;
                vBoardData['load'] = null;
                func_getData();
            } else {
                editDialogVisible.value = true;
            }
        };
        // 主业务处理 ========================================================================================= END
        /**
         * @name 删除数据
         */
        const func_del = (id) => {
            deleteBoardInfoById(id).then((res) => {
                if(res.code != 200) {
                    if(res.type === null){
                        ElMessage.error(res.message);
                    }else if(res.type === 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success("删除成功");
                    boardTableData.value.splice(index, 1);
                    func_getData();
                }
            });
        };
        // =========================================================================================================
        return {
            // 系统环境变量 =========================================================================================
            optColumnWidth, query, boardTableData, dataTotal, 
            // 主业务调用方法 =========================================================================================
            func_handleSearch, func_handlePageChange, func_handleDelete, func_handleAdd, handleEdit,
            // 数据执行处理 =========================================================================================
            func_getData, func_del,
			// 业务插件处理 =========================================================================================
            vBoardData, editDialogVisible, func_loadvBoardData,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>